package open.panxiong.shared.util

import android.content.Context
import android.content.SharedPreferences


class ConfigUtil(var context: Context) {
    companion object {
        private const val DEFAULT_NAME = "and_share_default"
        private lateinit var sharedPreferences: SharedPreferences
        private lateinit var sharedPreferencesEditor: SharedPreferences.Editor
    }

    init {
        sharedPreferences = context.getSharedPreferences(DEFAULT_NAME, Context.MODE_PRIVATE)
        sharedPreferencesEditor = sharedPreferences.edit()
    }

    fun getString(key: String, defValue: String = ""): String {
        return sharedPreferences.getString(key, defValue)!!
    }

    fun setString(key: String, value: String = "") {
        sharedPreferencesEditor.putString(key, value).commit()  // .apply() // 异步提交
    }

    fun getBoolean(key: String, defValue: Boolean = false): Boolean {
        return sharedPreferences.getBoolean(key, defValue)
    }

    fun setBoolean(key: String, defValue: Boolean = false): Boolean {
        return sharedPreferencesEditor.putBoolean(key, defValue).commit()
    }

}